package work20241217;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author zdl
 * @date 2024/12/17
 */
class Stu {
    private String name;
    private Integer age;

    public Stu() {
    }

    public Stu(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * 设置
     *
     * @param age
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    public String toString() {
        return "Stu{name = " + name + ", age = " + age + "}";
    }
}

class MyComparator implements Comparator<Stu> {

    @Override
    public int compare(Stu o1, Stu o2) {

        return o1.getAge().compareTo(o2.getAge()) != 0 ? o1.getAge().compareTo(o2.getAge()) : o1.getName().compareTo(o2.getName());
    }
}

public class Demo2 {
    public static void main(String[] args) {
        Set<Stu> set = new TreeSet<>(new MyComparator());
        set.add(new Stu("a", 16));
        set.add(new Stu("c", 15));
        set.add(new Stu("f", 13));
        set.add(new Stu("b", 13));
        set.add(new Stu("e", 11));
        System.out.println(set);
    }
}
